//订单操作模块
//数据操作模块
var sqlHelper = require('../conf/sqlHelper');

//把check中的商品数据加载出来
//params是数组参数
function getConfirmorder(customer_Id,params,callback) {
	var sqlstr='SELECT * from shop_cart sc,shop_product sp where sc.product_Id=sp.product_Id AND sc.customer_Id=? AND sc.product_Id in (?)';
    var param=[customer_Id,params];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
    	if(results){   
    		callback(results);
    	}    		
    });
}

//生成订单数据
function createOrder(orderInfo,callback) {
	var sqlstr="Call addOrder(?,?,?,?,?,?,?,?,?,',')";
    var param=[orderInfo.getcustomer_Id(),orderInfo.getaddress_Id(),orderInfo.getpdIdList(),orderInfo.getpdNumList(),orderInfo.getmessage(),orderInfo.getexpress_fee(),orderInfo.getcheck_invoices(),orderInfo.getpay_type(),orderInfo.gettotalprice()];

    sqlHelper.ExecuteUpdate(sqlstr,param,function(counts){
    	if(counts > 0) {
			callback(true);
		} else {
			callback(false);
		}		
    });
}

//获取刚生成的订单信息用于付款
function getNewOrderByCustomerId(customer_Id,callback) {
    var sqlstr='SELECT * FROM shop_order so,shop_orderinfo soi where so.order_Id=soi.order_Id AND so.customer_Id=? ORDER BY so.order_time DESC LIMIT 1';
    var param=[customer_Id];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){ 
           
        if(results){   
            callback(results);
        }           
    });
}

//根据登录用户ID和订单ID，打印确认付款的订单信息
function getOrderByCustomerIdAndOrderId(customer_Id,order_Id,callback) {
    var sqlstr='SELECT * FROM shop_order so,shop_orderinfo soi where so.order_Id=soi.order_Id AND so.customer_Id=? and so.order_Id=?';
    var param=[customer_Id,order_Id];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){            
        if(results){   
            callback(results);
        }           
    });
}

//支付操作根据用户ID和订单ID修改订单支付状态
function payOrderStatusByCustomerIdAndOrderId(customer_Id,order_Id,callback){
    var sqlstr='UPDATE shop_order SET order_status=1 where customer_Id=? and order_Id=?';
    var param=[customer_Id,order_Id];    

    sqlHelper.ExecuteUpdate(sqlstr,param,function(counts){
        if(counts > 0) {
            callback(true);
        } else {
            callback(false);
        }       
    });
}



//用户加载个人订单数据信息
function getOrderByCustomerId(customer_Id,callback) {
    var sqlstr='SELECT so.order_Id,so.order_code,so.order_time,so.order_status,soi.*,sopr.product_Id,sosp.product_img,sosp.product_name,sosp.new_price,sosp.product_favourable,sosp.product_expressfee,sopr.num,DATE_FORMAT(so.order_time,"%Y-%m-%d %H:%i:%s") as formateorder_time from shop_order so,shop_order_pro_rel sopr,shop_orderinfo soi,shop_order_snapshot_product sosp where so.order_Id=sopr.order_Id AND so.order_Id=soi.order_Id and sosp.order_Id=so.order_Id and sosp.product_Id=sopr.product_Id and so.customer_Id=? ORDER BY so.order_time DESC';
    var param=[customer_Id];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){ 
           
        if(results){   
            callback(results);
        }           
    });
}

//加载用户最近订单信息数据信息
function getNearlyOrderByCustomerId(customer_Id,topN,callback) {
    var sqlstr='SELECT so.order_Id,so.order_code,so.order_time,so.order_status,soi.*,sopr.product_Id,sosp.product_img,sosp.product_name,sosp.new_price,sosp.product_favourable,sosp.product_expressfee,sopr.num,DATE_FORMAT(so.order_time,"%Y-%m-%d %H:%i:%s") as formateorder_time from shop_order so,shop_order_pro_rel sopr,shop_orderinfo soi,shop_order_snapshot_product sosp where so.order_Id=sopr.order_Id AND so.order_Id=soi.order_Id and sosp.order_Id=so.order_Id and sosp.product_Id=sopr.product_Id and so.order_Id in (SELECT tmp.order_Id from ((select * from shop_order where shop_order.customer_Id=? AND shop_order.order_status<>3 ORDER BY shop_order.order_time DESC LIMIT ?) as tmp)) ORDER BY formateorder_time DESC';
      
    var param=[customer_Id,topN];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){ 
        console.log('test:'+results);  
        //console.log('test:'+results[0].order_code);     
        if(results){   
            callback(results);
        }           
    });
}

//未付款的订单数量查询方法
function noPayOrderlistByCustomerId(customer_Id,callback){
    var sqlstr='SELECT count(*) as counts from shop_order so,shop_order_pro_rel sopr,shop_orderinfo soi,shop_order_snapshot_product sosp where so.order_Id=sopr.order_Id AND so.order_Id=soi.order_Id and sosp.order_Id=so.order_Id and sosp.product_Id=sopr.product_Id and so.order_status=0 and so.customer_Id=? ORDER BY so.order_time DESC';
    var param=[customer_Id];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){ 
           
        if(results){   
            callback(results[0].counts);
        }           
    });
}

//未收货的订单数量查询方法
function noReceiveOrderlistByCustomerId(customer_Id,callback){
    var sqlstr='SELECT count(*) as counts from shop_order so,shop_order_pro_rel sopr,shop_orderinfo soi,shop_order_snapshot_product sosp where so.order_Id=sopr.order_Id AND so.order_Id=soi.order_Id and sosp.order_Id=so.order_Id and sosp.product_Id=sopr.product_Id and so.order_status<3 and so.customer_Id=? ORDER BY so.order_time DESC';
    var param=[customer_Id];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){ 
           
        if(results){   
            callback(results[0].counts);
        }           
    });
}

//获取订单分页
function getOrderListPager(currentPage,pageSize,callback){
    //计算起始数据
    //起始计数从0开始
    var startIndex=(currentPage-1)*pageSize;

    var sqlstr='SELECT *,DATE_FORMAT(so.order_time,"%Y-%m-%d %H:%i:%s") as formateorder_time FROM shop_order so,shop_orderinfo soi where so.order_Id=soi.order_Id LIMIT ?,?';
    var param=[startIndex,pageSize];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
        if(results){
            //console.log('results:'+results);
            callback(results);
        }
    }); 
}

//获取订单总行数
function getOrdersCounts(callback){
    var sqlstr='select count(*) as count from shop_order';

    sqlHelper.ExecuteReader(sqlstr,null,function(results){
        if(results){
            //console.log('results:'+results[0].count);
            callback(results[0].count);
        }
    }); 
}

//根据搜索关键字获取订单列表分页数据
function getSearchOrderListPager(searchkey,currentPage,pageSize,callback){
    //计算起始数据
    //起始计数从0开始
    var startIndex=(currentPage-1)*pageSize;

    var sqlstr='SELECT *,DATE_FORMAT(so.order_time,"%Y-%m-%d %H:%i:%s") as formateorder_time FROM shop_order so,shop_orderinfo soi where so.order_Id=soi.order_Id and so.order_code like? LIMIT ?,?';
    var param=['%'+searchkey+'%',startIndex,pageSize];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
        if(results){
            //console.log('results:'+results);
            callback(results);
        }
    }); 
}

//根据搜索关键字获取订单列表行数
function getSearchOrdersCounts(searchkey,callback){
    var sqlstr='select count(*) as count from shop_order where order_code like ?';
    var param=['%'+searchkey+'%'];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
        if(results){
            //console.log('results:'+results[0].count);
            callback(results[0].count);
        }
    }); 
}

//根据登录用户ID获取当前用户的订单总数
function getOrdersCountsByCostomerID(customer_Id,callback){
    var sqlstr='select count(*) as count from shop_order where customer_Id=?';
    var param=[customer_Id];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
        if(results){
            console.log('results:'+results[0].count);
            callback(results[0].count);
        }
    }); 
}

exports.getConfirmorder=getConfirmorder;
exports.createOrder=createOrder;
exports.getOrderByCustomerId=getOrderByCustomerId;
exports.getNearlyOrderByCustomerId=getNearlyOrderByCustomerId;
exports.noPayOrderlistByCustomerId=noPayOrderlistByCustomerId;
exports.noReceiveOrderlistByCustomerId=noReceiveOrderlistByCustomerId;
exports.getNewOrderByCustomerId=getNewOrderByCustomerId;
exports.getOrderByCustomerIdAndOrderId=getOrderByCustomerIdAndOrderId;
exports.payOrderStatusByCustomerIdAndOrderId=payOrderStatusByCustomerIdAndOrderId;
exports.getOrderListPager=getOrderListPager;
exports.getOrdersCounts=getOrdersCounts;
exports.getSearchOrderListPager=getSearchOrderListPager;
exports.getSearchOrdersCounts=getSearchOrdersCounts;
exports.getOrdersCountsByCostomerID=getOrdersCountsByCostomerID;